/*
 * File:   Image.h
 * Author: bherila
 *
 * Created on March 6, 2010, 12:33 AM
 */

#ifndef _IMAGE_H
#define	_IMAGE_H

#include <cv.h>

/**
 * Represents a RGB pixel.
 */
typedef struct{
  unsigned char b,g,r;
} RgbPixel;

/**
 * Represents a floating point pixel intensity. The values range from 0 (black)
 * to 1 (full intensity) in each color channel.
 */
typedef struct{
  float b,g,r;
} RgbPixelFloat;


template<class T>
class Image
{
  protected:
    IplImage* imgp;
  public:

      /**
       * @brief This is the constructor for our IplImage wrapper.
       * @param img the IplImage to wrap
       */
      Image(IplImage* img=0) {
          imgp=img;
      }

      /**
       * @brief Class destructor.
       */
      ~Image(){
          imgp=0;
      };

      /**
       * @brief Overloads the equals operator to provide two way access
       * to the underlying IplImage.
       *
       * @param img  the IplImage to set
       */
      void operator=(IplImage* img) {
          imgp=img;
      };

      /**
       * @brief Overloads the bracket operator to allow normal pixel-based
       * indexing into the 2D image array.
       *
       * Returns a pointer to the start of the given row.
       *
       * @param rowIndx the index of the row in the image
       */
      inline T* operator[](const int rowIndx) {
        return ((T *)(imgp->imageData + rowIndx*imgp->widthStep));
      }
      
      /**
       * @brief Returns a copy of the RgbPixel at the given row and column in this image.
       *
       * @param row the index of the row in the image
       * @param col the index of the column in the image
       */
      RgbPixel getPixel(int row, int col) const {
	CvScalar tmp;
	tmp = cvGet2D(imgp, row, col);
	RgbPixel p;
	p.b = tmp.val[0];
	p.g = tmp.val[1];
	p.r = tmp.val[2];
	return p;
      }

};

// Various type definitions touse as shortcuts.

typedef Image<RgbPixel>       RgbImage;
typedef Image<RgbPixelFloat>  RgbImageFloat;
typedef Image<unsigned char>  BwImage;
typedef Image<float>          BwImageFloat;



#endif	/* _IMAGE_H */
